using Microsoft.AspNetCore.Mvc;
using MVCWeekOne.Models;
using System.Text;
using System.Text.Json;

namespace MVCWeekOne.Controllers
{
    public class ProcessController : Controller
    {
        private readonly HttpClient _httpClient;
        private readonly string _baseUrl = "http://localhost:5089/api";

        public ProcessController()
        {
            _httpClient = new HttpClient();
        }

        /// <summary>
        /// 工序管理页面
        /// </summary>
        public IActionResult Index()
        {
            ViewData["Title"] = "工序管理";
            return View();
        }

        /// <summary>
        /// 获取工序分页数据
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetProcessPage([FromBody] ProcessQueryModel query)
        {
            try
            {
                var json = JsonSerializer.Serialize(query);
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                
                var response = await _httpClient.PostAsync($"{_baseUrl}/Process/query", content);
                var result = await response.Content.ReadAsStringAsync();
                
                return Content(result, "application/json");
            }
            catch (Exception ex)
            {
                return Json(new { code = 500, msg = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 获取工序采集数据
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetProcessCollectionData(long processId)
        {
            try
            {
                var response = await _httpClient.GetAsync($"{_baseUrl}/Process/collection-data/{processId}");
                var result = await response.Content.ReadAsStringAsync();
                
                return Content(result, "application/json");
            }
            catch (Exception ex)
            {
                return Json(new { code = 500, msg = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 获取不良品项列表
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetDefectiveList(int index = 1, int size = 100, string defectiveCode = "", string defectiveName = "")
        {
            try
            {
                var url = $"{_baseUrl}/Defective/GetPage?Index={index}&Size={size}";
                if (!string.IsNullOrEmpty(defectiveCode))
                    url += $"&DefectiveCode={defectiveCode}";
                if (!string.IsNullOrEmpty(defectiveName))
                    url += $"&DefectiveName={defectiveName}";

                var response = await _httpClient.GetAsync(url);
                var result = await response.Content.ReadAsStringAsync();
                
                return Content(result, "application/json");
            }
            catch (Exception ex)
            {
                return Json(new { code = 500, msg = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetRoleAll()
        {
            try
            {
                Console.WriteLine($"开始调用角色API: {_baseUrl}/Role/GetRoleAll");
                
                var response = await _httpClient.GetAsync($"{_baseUrl}/Role/GetRoleAll");
                Console.WriteLine($"角色API响应状态: {response.StatusCode}");
                
                var result = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"角色API返回结果: {result}");
                
                return Content(result, "application/json");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"角色API调用异常: {ex.Message}");
                Console.WriteLine($"异常堆栈: {ex.StackTrace}");
                return Json(new { code = 500, msg = $"查询失败：{ex.Message}" });
            }
        }
    }
} 